/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrameHistorico.java
 *
 * Created on 10/10/2011, 10:47:42
 */
package br.com.GUI.cadastro;

import br.com.classes.Historico;
import br.com.dao.HistoricoDao;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

/**
 *
 * @author Kauane
 */
public class FrameHistorico extends javax.swing.JFrame {

    /** Creates new form FrameHistorico */
    int cod_historico;
    int num = 0;
    ArrayList<Integer> valores = new ArrayList<Integer>(); 

    public FrameHistorico() {
        initComponents();
        CarregarHistorico();
    }

        public class LetraMaiuscula extends PlainDocument {  
     @Override  
     public void insertString(int offset, String str, AttributeSet attr)  
     throws BadLocationException {  
       super.insertString(offset, str.toUpperCase(), attr);  
     }    
}  
    
    public void CarregarHistorico() {
        HistoricoDao bdao = new HistoricoDao();
        List<Historico> lista = bdao.ListarHistoricos();
        DefaultTableModel modelo = (DefaultTableModel) jTableConsultaHistorico.getModel();
        modelo.setRowCount(0);
        for (Historico h : lista) {
            modelo.addRow(new Object[]{h.getCod_historico(), h.getHistorico()});
        }
        jTableConsultaHistorico.setModel(modelo);
    }
    

    public void CarregarHistoricoLetra() {
        String hist = jTextField11.getText();

        HistoricoDao hdao = new HistoricoDao();
        List<Historico> lista = hdao.ProcurarHistoricoLetra(hist);

        DefaultTableModel modelo = (DefaultTableModel) jTableConsultaHistorico.getModel();
        modelo.setRowCount(0);

        for (Historico h : lista) {

            modelo.addRow(new Object[]{h.getCod_historico(), h.getHistorico()});
        }

        jTableConsultaHistorico.setModel(modelo);
    }

    public void BuscarLimiteAnterior() {
        num = num - 10;

        HistoricoDao hdao = new HistoricoDao();
        List<Historico> lista = hdao.BuscarLimite(num);

        DefaultTableModel modelo = (DefaultTableModel) jTableConsultaHistorico.getModel();
        modelo.setRowCount(0);

        for (Historico h : lista) {

            modelo.addRow(new Object[]{h.getCod_historico(), h.getHistorico()});
        }
        jTableConsultaHistorico.setModel(modelo);
                        if(lista.size()<10){
    jButtonVoltar.setEnabled(false);
    }
    }

    public void BuscarLimiteProximo() {
        num = num + 10;

        HistoricoDao hdao = new HistoricoDao();
        List<Historico> lista = hdao.BuscarLimite(num);

        DefaultTableModel modelo = (DefaultTableModel) jTableConsultaHistorico.getModel();
        modelo.setRowCount(0);

        for (Historico h : lista) {

            modelo.addRow(new Object[]{h.getCod_historico(), h.getHistorico()});
        }
        jTableConsultaHistorico.setModel(modelo);
                        if(lista.size()<10){
    jButtonAvancar.setEnabled(false);
    }
    }

    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel12 = new javax.swing.JLabel();
        jTextField11 = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableConsultaHistorico = new javax.swing.JTable();
        jButtonVoltar = new javax.swing.JButton();
        jButtonEditar = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButtonAvancar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButtonCadastrar = new javax.swing.JButton();
        jLabelHistorico = new javax.swing.JLabel();

        setTitle("Histórico Padrão");

        jLabel12.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel12.setText("Pesquisa Historico");

        jTextField11.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextField11KeyReleased(evt);
            }
        });

        jLabel13.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/search[1].png"))); // NOI18N

        jTableConsultaHistorico.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Histórico Padrão"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableConsultaHistorico.getTableHeader().setReorderingAllowed(false);
        jTableConsultaHistorico.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableConsultaHistoricoMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTableConsultaHistorico);
        jTableConsultaHistorico.getColumnModel().getColumn(0).setMinWidth(50);
        jTableConsultaHistorico.getColumnModel().getColumn(0).setPreferredWidth(50);
        jTableConsultaHistorico.getColumnModel().getColumn(0).setMaxWidth(50);
        jTableConsultaHistorico.getColumnModel().getColumn(1).setMinWidth(600);
        jTableConsultaHistorico.getColumnModel().getColumn(1).setPreferredWidth(600);
        jTableConsultaHistorico.getColumnModel().getColumn(1).setMaxWidth(600);

        jButtonVoltar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/back[2].png"))); // NOI18N
        jButtonVoltar.setBorder(null);
        jButtonVoltar.setContentAreaFilled(false);
        jButtonVoltar.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/back[2]over.png"))); // NOI18N
        jButtonVoltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVoltarActionPerformed(evt);
            }
        });

        jButtonEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/pencil.png"))); // NOI18N
        jButtonEditar.setBorder(null);
        jButtonEditar.setContentAreaFilled(false);
        jButtonEditar.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/pencil[2]over.png"))); // NOI18N
        jButtonEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEditarActionPerformed(evt);
            }
        });

        jButton8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/refresh.png"))); // NOI18N
        jButton8.setBorder(null);
        jButton8.setContentAreaFilled(false);
        jButton8.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/refresh[2]over.png"))); // NOI18N
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        jButton3.setBackground(null);
        jButton3.setForeground(null);
        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/delete[5].png"))); // NOI18N
        jButton3.setBorder(null);
        jButton3.setBorderPainted(false);
        jButton3.setContentAreaFilled(false);
        jButton3.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/delete[5]over.png"))); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButtonAvancar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/next[2].png"))); // NOI18N
        jButtonAvancar.setBorder(null);
        jButtonAvancar.setContentAreaFilled(false);
        jButtonAvancar.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/next[2]over.png"))); // NOI18N
        jButtonAvancar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAvancarActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("*Histórico:");

        jTextField1.setDocument(new LetraMaiuscula());

        jButtonCadastrar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jButtonCadastrar.setText("Cadastrar");
        jButtonCadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCadastrarActionPerformed(evt);
            }
        });

        jLabelHistorico.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabelHistorico.setText("Historico");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabelHistorico)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 382, Short.MAX_VALUE)
                        .addComponent(jLabel12)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField11, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel13)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButtonVoltar)
                        .addGap(10, 10, 10)
                        .addComponent(jButtonEditar)
                        .addGap(12, 12, 12)
                        .addComponent(jButton8)
                        .addGap(10, 10, 10)
                        .addComponent(jButton3)
                        .addGap(10, 10, 10)
                        .addComponent(jButtonAvancar)
                        .addGap(258, 258, 258))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 460, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButtonCadastrar, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 652, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(jLabelHistorico))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel12)
                                    .addComponent(jTextField11))
                                .addGap(4, 4, 4))
                            .addComponent(jLabel13, javax.swing.GroupLayout.Alignment.LEADING))))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonCadastrar))
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonVoltar)
                    .addComponent(jButtonEditar)
                    .addComponent(jButton8)
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonAvancar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(23, 23, 23))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonVoltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonVoltarActionPerformed
        BuscarLimiteAnterior();
        jButtonAvancar.setEnabled(true);
}//GEN-LAST:event_jButtonVoltarActionPerformed

    private void jButtonEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEditarActionPerformed

        if (jTableConsultaHistorico.getSelectedRow() >= 0) {
            if (jLabelHistorico.getText() == "Historico") {
                jButtonEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/removeicon.png")));
                jButtonEditar.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/removeicon.png")));
            int k = jTableConsultaHistorico.getSelectedRow();
            cod_historico = (Integer) jTableConsultaHistorico.getValueAt(k, 0);

            HistoricoDao hdao = new HistoricoDao();
            Historico h = hdao.PesquisarHistorico(cod_historico);

            jTextField1.setText(h.getHistorico());
            jLabelHistorico.setText("Alterar Historico");
            jButtonCadastrar.setText("Alterar");
            }else{
                        jTextField1.setText("");
                        jTextField1.requestFocus();
                        jLabelHistorico.setText("Historico");
                        jButtonCadastrar.setText("Cadastrar");  
                          jButtonEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/pencil.png")));
                jButtonEditar.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/pencil[2]over.png")));
            }           
            } else {
            JOptionPane.showMessageDialog(this, "Favor selecionar o Historico para ser alterado");
        }
}//GEN-LAST:event_jButtonEditarActionPerformed

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
     CarregarHistorico();
     jButtonAvancar.setEnabled(true);
}//GEN-LAST:event_jButton8ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
 if (jTableConsultaHistorico.getSelectedRow() >= 0) {
        int linha = jTableConsultaHistorico.getSelectedRow();
        Object[] OPTIONS = {"Sim", "Não"};
        String historico = jTableConsultaHistorico.getValueAt(linha, 1).toString();
        HistoricoDao hdao = new HistoricoDao();
        Historico h = new Historico();
        h.setHistorico(historico);
        try {
            int I = JOptionPane.showOptionDialog(null, "Tem certeza que deseja excluir o registro?", "Excluir", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, OPTIONS, OPTIONS[0]);
            if (I == JOptionPane.YES_OPTION) {

                hdao.DeletarHistorico(h);
                JOptionPane.showMessageDialog(this, "Excluído com sucesso!");
                CarregarHistorico();
            }
        } catch (Exception e) {
            System.out.println("1 - " + e.getMessage());
        }}else {
           JOptionPane.showMessageDialog(null, "Por favor selecione um historico para ser excluido!");
       }   
}//GEN-LAST:event_jButton3ActionPerformed

    private void jButtonAvancarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAvancarActionPerformed
        BuscarLimiteProximo();
        jButtonVoltar.setEnabled(true);
}//GEN-LAST:event_jButtonAvancarActionPerformed

    private void jButtonCadastrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCadastrarActionPerformed
        if (jTextField1.getText().isEmpty()) {
            JOptionPane.showMessageDialog(this, "Favor preencher os campos!");
        } else {

            try {
                if (jLabelHistorico.getText().equals("Historico")) {
                    Historico h = new Historico();
                    HistoricoDao hdao = new HistoricoDao();
                    int codhis = hdao.UltimoCodHistorico();

                    //   DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
                    h.setHistorico(jTextField1.getText());

                    hdao.CadastrarHistorico(h);
                    CarregarHistorico();

                    JOptionPane.showMessageDialog(this, "Cadastro realizado");
                    jTextField1.setText("");
                    jTextField1.requestFocus();
                } else {

                    Object[] OPTIONS = {"Sim", "Não"};

                    int I = JOptionPane.showOptionDialog(null, "Tem certeza que deseja alterar o registro?", "Alterar", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, OPTIONS, OPTIONS[0]);

                    if (I == JOptionPane.YES_OPTION) {
                        Historico h = new Historico();
                        h.setCod_historico(cod_historico);
                        h.setHistorico(jTextField1.getText());

                        HistoricoDao hdao = new HistoricoDao();
                        hdao.AlterarHistorico(h);

                        jTextField1.setText("");
                        jTextField1.requestFocus();
                        jLabelHistorico.setText("Historico");
                        jButtonCadastrar.setText("Cadastrar");
                        jButtonEditar.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/pencil[2]over.png")));
                        jButtonEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/pencil.png")));

                    }
                    CarregarHistorico();
                }
            } catch (Exception ex) {
                System.out.println("1 - " + ex.getMessage());

            }
        }
    }//GEN-LAST:event_jButtonCadastrarActionPerformed

    private void jTextField11KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField11KeyReleased
        // TODO add your handling code here:
        CarregarHistoricoLetra();
    }//GEN-LAST:event_jTextField11KeyReleased

    private void jTableConsultaHistoricoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableConsultaHistoricoMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jTableConsultaHistoricoMouseClicked
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FrameHistorico().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButtonAvancar;
    private javax.swing.JButton jButtonCadastrar;
    private javax.swing.JButton jButtonEditar;
    private javax.swing.JButton jButtonVoltar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabelHistorico;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableConsultaHistorico;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField11;
    // End of variables declaration//GEN-END:variables
}
